package com.petmagics.learnhttp;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Post方式读取数据
 */
public class HttpPostFm extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_http_post, container, false);
        root.findViewById(R.id.btnReadData).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new AsyncTask<String, Void, Void>() {
                    HttpURLConnection conn;
                    InputStream is;
                    InputStreamReader isr;
                    BufferedReader br;
                    @Override
                    protected Void doInBackground(String... strings) {
                        try {
                            URL url = new URL(strings[0]);
                            conn = (HttpURLConnection) url.openConnection();
                            conn.setDoOutput(true); //输出数据  setDoInput()为默认使用
                            conn.setRequestMethod("POST");
                            OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
                            BufferedWriter bw = new BufferedWriter(osw);
                            bw.write("keyfrom=testHttpTransfer&key=1263769019&type=data&doctype=xml&version=1.1&q=good");
                            bw.flush();
                            is = conn.getInputStream();
                            isr = new InputStreamReader(is, "UTF-8");
                            br = new BufferedReader(isr);
                            String line;
                            while ((line = br.readLine()) != null) {
                                System.out.println(line);
                            }
                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                br.close();
                                isr.close();
                                is.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        return null;
                    }
                }.execute("http://fanyi.youdao.com/openapi.do");
            }
        });
        return root;
    }
}
